<?php

/*
 * litchicms
 * Copyright (C) 2010-5-13 Author: tiefa(539247@qq.com)
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
!defined('BRK') && exit("!");

/**
 * 做管理员操作日志
 * @param  $log_string
 */
function brk_bglog($log_string) {
    global $BRK_GLOBAL;
    check_make_dir($dir = SYSTEM . "log/bg/" . $BRK_GLOBAL['datearray']['year'] . '/' . $BRK_GLOBAL['datearray']['mon']);
    if (!is_file($file = $dir . '/' . $BRK_GLOBAL['datearray']['mday'] . ".php")) {
        if (file_put_contents($file, "<?php exit; ?>\n") == 0)
            exit('!fwrite --' . $file . ' -- bg.fun.php');
    }
    $logstr = '['
            . $BRK_GLOBAL['datearray']['year'] . '-' . $BRK_GLOBAL['datearray']['mon'] . '-' . $BRK_GLOBAL['datearray']['mday'] . ' '
            . $BRK_GLOBAL['datearray']['hours'] . ':' . $BRK_GLOBAL['datearray']['minutes'] . ':' . $BRK_GLOBAL['datearray']['seconds']
            . ']' . ( is_array($log_string) ? implode("\n ", $log_string) : "\n" . $log_string );

    $handle = fopen($file, "a");
    if (fwrite($handle, $logstr . "\n\n") === FALSE)
        exit('fwrite --' . $file . ' -- bg.fun.php'); // "不能写入到文件 $file";
 fclose($handle);
    return TRUE;
}

/**
  后台message
 *
 */
function eMsg($isok=0, $data=null, $url=0) {
    $GLOBALS['BRK_MSG'][0] = $isok;
    if ($data != null) {
        $GLOBALS['BRK_MSG'][1][] = $data;
    }
    if ($url != 0) {
        $GLOBALS['BRK_MSG'][2] = $url;
    }
}

/*
  STR + $_SESSION['BASE64']  BASE64
 */

function base64($str) {
    if (!key_exists('BASE64', $_SESSION)) {
        unset($_SESSION);
        header301("?index?admin");
    }
    return base64_encode($_SESSION['BASE64'] . $str) . '/';
}

/*
 * 获取$RT['m'] $RT['s'] $RT['a']
 */

function brk_get_rt($s1) {
    global $RT;
    switch ($s1) {
        case 'm':
//          dump(brk_right_get_child(2));
            $RT['r']['m'] = ($temp = brk_right_get_child(2)) ? $temp[0] : msg("你没有权限进行此操作");
            $RT['v']['m'] = brk_right_get_node($RT['r']['m']);
            break;
        case 's':
//            dump($RT);
//            dump($GLOBALS['BRK_GLOBAL']['RIGTHS']);
//            dump(brk_right_get_child($RT['r']['m']));
//            $RT['r']['s'] = ($temp = brk_right_get_child($RT['r']['m'])) ? $temp[0] : msg("你没有权限进行此操作");
            $RT['r']['s'] = ($temp = brk_right_get_child($RT['r']['m'])) ? $temp[0] : '-2';
            $RT['v']['s'] = brk_right_get_node($RT['r']['s']);
            break;
        case 'a':

//            $RT['r']['a'] = ($temp = brk_right_get_child($RT['r']['s'])) ? $temp[0] : msg("你没有权限进行此操作");
            $RT['r']['a'] = ($temp = brk_right_get_child($RT['r']['s'])) ? $temp[0] : '-2';
            $RT['v']['a'] = brk_right_get_node($RT['r']['a']);

            break;
        default:
            break;
    }
    return TRUE;
}

/*
 * 获取RT pre link
 */

function bgpre($str='') {
    return $GLOBALS['BREADCRUMD'][0]['node'] . $str;
}

/*
 * 打印提示消息
 */

function bgmsg($data) {
    echo "<br/>";
    if (is_array($data)) {
        foreach ($data as $v) {
            echo "&nbsp;&nbsp;&nbsp;&nbsp;" . $v . "<br/>";
        }
    } else {
        echo "&nbsp;&nbsp;&nbsp;&nbsp;" . $data;
    }
}

// CAT:bg - member - rights

/**
 * 添加权限
 * 
 */
function bg_add_right($pid, $right_note, $right_node) {
    $dataarr = array(
        'right_pid' => $pid,
        'right_note' => $right_note,
        'right_node' => $right_node
    );
    if ($rs = brk_insert("right", $dataarr)) {
        return $rs;
    }
    return FALSE;
}

/**
 * 删除权限
 * @param $id 要删除的权限id
 * @param $tree 权限树
 */
function bg_del_right($id, $tree=FALSE) {
    !class_exists("Tree") && require 'tree.class.php';
    if (!is_object($tree) || $tree instanceof Tree) {
        $rs = brk_select("right");
        while ($row = $GLOBALS['Db']->fetch_array($rs)) {
            $RIGHT[$row['right_id']] = $row;
        }
        $tree = new Tree($RIGHT, 'right_id', 'right_pid', 'right_note');
    }

    if ($arr = $tree->getChilds($id))
        if (brk_delete('right', "WHERE right_id in (" . implode(',', $arr) . ")"))
            return TRUE;


    return FALSE;
}

/*
 * brk_get_all_pageset
 */

function brk_get_all_pageset() {
    if (is_file(SYSTEM . 'cache/pagevarset.php')) {
        require SYSTEM . 'cache/pagevarset.php';
        return $pagevarset;
    }
    global $Db;
    $rs = brk_select('settings_diy', "*", "WHERE diy_ispage=1");
    $all_pageset = array();
    while ($row = $Db->fetch_array($rs)) {
        $all_pageset[] = array(
            'diy_key' => $row['diy_key'],
            'diy_value' => $row['diy_type'] == 'array' ? unserialize($row['diy_value']) : $row['diy_value'],
            'diy_name' => $row['diy_name']
        );
    }
    $data = "<?php\n\$pagevarset=" . var2string($all_pageset) . ';';
    file_put_contents(SYSTEM . 'cache/pagevarset.php', $data);
    return $all_pageset;
}

/**
 * 组建当前的页面域变量
 */
function brk_build_this_pageset_array($str) {
    if (empty($str)) {
        $this_pageset = array();
    } else {
        $this_pageset = unserialize($str);
    }
    $all_pageset = brk_get_all_pageset();
    $str = "";
    if (empty($all_pageset)) {
        return array();
    } else {
        $returnarray = array();
        foreach ($all_pageset as $value) {
            $temp = empty($this_pageset[$value['diy_key']]) ? $value['diy_value'] : $this_pageset[$value['diy_key']];
            $returnarray[] = array(
                'diy_key' => $value['diy_key'],
                'diy_name' => $value['diy_name'],
                'diy_value' => $temp
            );
        }
        return $returnarray;
    }
}
